﻿using Newtonsoft.Json;

namespace QingStor_SDK_NET.Common
{
    // Json Tools
    public static class CJsonUtils
    {
        // From Object to Json
        public static string ObjectToJson(object Obj)
        {
            if (Obj == null)
            {
                return "";
            }

            string strJson = JsonConvert.SerializeObject(Obj, Formatting.Indented, 
                                                         new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

            return strJson;
        }

      
        // From Json to Object
        public static T JsonToObject<T>(string strJson) 
            where T: class
        {
            if (string.IsNullOrEmpty(strJson))
            {
                return null;
            }
            return JsonConvert.DeserializeObject<T>(strJson);
        }
    }
}
